/**
 * goio-util
 *
 * This class do 
 *
 * @link http://www.marcosperon.com/
 * @copyright 2009 - Peron Co.
 * @author Marcos Peron
 * @package com.marcosperon.util.java
 * @version 2009
 */
package com.mperon.util.java;

import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * The Class ClassPathLoader.
 * 
 * @author Marcos Peron
 */
public class ClassPathLoader {

    /** The Constant parameters. */
    private static final Class<?>[] PARAMETERS = new Class[] { URL.class };

    /**
     * Instantiates a new class path loader.
     */
    private ClassPathLoader() {
    }

    /**
     * Adds the to classpath.
     * 
     * @param u the u
     * 
     * @throws IOException Signals that an I/O exception has occurred.
     */
    public static void addToClasspath(final URL u) throws IOException {
        final URLClassLoader sysloader = (URLClassLoader)ClassLoader.getSystemClassLoader();
        final Class<?> sysclass = URLClassLoader.class;

        try {
            final Method method = sysclass.getDeclaredMethod("addURL", PARAMETERS);
            method.setAccessible(true);
            method.invoke(sysloader, new Object[] { u });
        } catch (final Throwable t) {
            throw new IOException("Error, could not add URL to system classloader");
        }
    }
}
